package com.example.qihuibao.widget.view

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ProgressBar
import android.widget.TextView
import com.example.qihuibao.R
import com.example.qihuibao.bean.RspQueryCanteenInfoBean
import com.example.qihuibao.bean.RspQueryEmployInfoBean
import com.example.qihuibao.bean.RspQueryMealInfoBean
import com.example.qihuibao.bean.RspQueryOrderInfoBean
import com.example.qihuibao.constant.Global
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.EnumUtils

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/13
 *     desc   :C屏：面向用户
 */
class CScreenLayout(ctx:Context) : FrameLayout(ctx) {
    private lateinit var root:View
    private lateinit var tvStatus:TextView
    private lateinit var tvToast:TextView
    private lateinit var tv_employ_name:TextView
    private lateinit var tv_employ_info:TextView
    private lateinit var tv_employ_order:TextView
    private lateinit var tv_canteen_info:TextView
    private lateinit var tv_meal_info:TextView
    init {
        root = LayoutInflater.from(ctx).inflate(R.layout.layout_cscreen, this, true)
        initView()
    }

    private fun initView() {
        tvStatus = root.findViewById(R.id.tv_status)
        tvToast = findViewById(R.id.pre_tv_toast)
        tv_employ_name = findViewById(R.id.tv_employ_name)
        tv_employ_info = findViewById(R.id.tv_employ_info)
        tv_employ_order = findViewById(R.id.tv_employ_order)
        tv_canteen_info = findViewById(R.id.tv_canteen_info)
        tv_meal_info = findViewById(R.id.tv_meal_info)
    }

    fun showDevInit() {
        tvStatus.text = "等待初始化"
    }

    fun showResult(result:String, message:String = "") {
        tvStatus.text = result
        Handler().postDelayed({
//            hideOrder()
            tvStatus.text = getUseMode()
                              }, 3000)
    }

    fun showCanteenInfo(bean:RspQueryCanteenInfoBean?) {
        tv_canteen_info.text = "餐厅：${bean?.CantName}\n" +
                "状态：${if (bean?.Status == "0") "正常" else "冻结"}\n" +
                "取餐：${getUseMode()}"
    }

    fun showMealInfo(bean:RspQueryMealInfoBean?) {
        tv_meal_info.text = "餐别：${bean?.MealName}\n" +
                "状态：${if (bean?.Status == "0") "不开餐" else "开餐"}\n" +
                "用餐开始时间：${bean?.UseBeginTime}\n" +
                "用餐结束时间：${bean?.UseEndTime}\n" +
                "支持配送：${if (bean?.DeliverFlag == "0") "否" else "是"}"
    }

    fun showLoading(show:Boolean, message:String = "") {
        tvStatus.text = message
    }

    fun showEmployInfo(bean: RspQueryEmployInfoBean.Employ?) {
        if (bean == null) {
            tv_employ_info.text = "找不到员工信息！"
        }else {
            tv_employ_name.text = "${bean.EmpName}"
            tv_employ_info.text =
                        "余额：${bean.Balance}\n" +
                        "状态：${if (bean.Status == "1") "正常" else (if (bean.Status == "2") "挂失" else "冻结")}"
        }
    }

    fun showEmployOrder(bean: RspQueryOrderInfoBean.Order?) {
        if (bean == null) {
            tv_employ_order.text = "找不到订餐信息！"
        }else {
            tv_employ_order.text =
                "订餐份数：${bean.Count}\n" +
                        "已取餐份数：${bean.UseCount}\n" +
                        "餐别：${bean.MealID}\n" +
                        "套餐：${bean.MealSetID}\n" +
                        "订餐时间：${bean.MealDate}-${bean.TranTime}\n" +
                        "订餐流水号：${bean.TranID}"
        }
    }

    fun getUseMode(): String {
        if (Global.currentCanteenInfoBean != null) {
            val bit1 = EnumUtils.getEnable(Global.currentCanteenInfoBean?.UseMode?:"", 1)//"出示取餐信息" else ""
            val bit2 = EnumUtils.getEnable(Global.currentCanteenInfoBean?.UseMode?:"", 2)//"扫码取餐" else ""
//            val bit3 = EnumUtils.getEnable(Global.currentCanteenInfoBean?.UseMode?:"", 3)//"扫码取餐" else ""
            if (bit1 && bit2) return "请出示取餐信息 / 扫码取餐"
            else if (bit1) return "请出示取餐信息"
            else if (bit2) return "扫码取餐"
        }
        return ""
    }


    //---------------C屏的toast------------------------------
    fun toast(msgRes: Int) {
        toast(resToString(msgRes))
    }

    fun toast(msg: String) {
        if (msg.isEmpty()) {
            return
        }
        mCurrentToastMsg = msg
        mHandler.post(mShowToastWork)
        mHandler.removeCallbacks(mCloseToastWork)
        mHandler.postDelayed(mCloseToastWork, 2000L)
    }

    private fun resToString(res: Int): String {
        return context.getString(res)
    }

    private val mHandler = Handler(Looper.getMainLooper())
    private var mCurrentToastMsg: String? = null
    //show toast
    private val mShowToastWork = Runnable {
        tvToast.visibility = View.VISIBLE
        tvToast.text = mCurrentToastMsg
    }

    //close toast
    private val mCloseToastWork = Runnable { tvToast.visibility = View.GONE }
    //---------------C屏的toast end------------------------------
}